package com.mwm.loyal.bean;

import com.mwm.loyal.util.GsonUtil;

import java.util.List;

public class ResultMap {

    private static ResultMap instance;
    private String code;
    private String message;
    private Object obj;
    private List<?> list;

    public static ResultMap getInstance() {
        if (null == instance)
            synchronized (ResultMap.class) {
                instance = new ResultMap();
            }
        return instance;
    }

    public ResultMap() {
    }

    public ResultMap setCode(String code) {
        this.code = code;
        return this;
    }

    public ResultMap setMessage(String message) {
        this.message = message;
        return this;
    }

    public String setThrow(Throwable throwable) {
        return set("-99", throwable.getMessage()).toJson();
    }

    public String setError(String message) {
        return set("-1", message).toJson();
    }

    public ResultMap set(String code, String message) {
        return setCode(code).setMessage(message);
    }

    public ResultMap setObj(Object obj) {
        this.obj = obj;
        return this;
    }

    public ResultMap setList(List<?> list) {
        this.list = list;
        return this;
    }

    public String toJson() {
        //String json = GsonUtil.obj2Json(map);
        return GsonUtil.obj2Json(this);
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getObj() {
        return obj;
    }

    public List<?> getList() {
        return list;
    }
}
